import {
    CHANGELOG,
    CHANGEUSER,
    CHANGECURRENTITEM,
    CHANGECURRENTROOM,
    CHANGECURRENTROOMINFO,
} from './constants'

/**
 * islogin: boolean
 * user: {
 *  id: number  0为系统消息
 *  gender: '0' || '1' || '2'   // 0女1男2保密
 *  introduction: string
 *  avator: string
 * }
 * currentItem: 'room' || 'info'
 * currentRoom: number
 * currentRoomInfo: {
 *  group_id: number    0为系统消息
 *  group_name: string
 *  group_intro: string
 *  group_avator: string
 *  creator_id: number
 *  admin: array || null        // 暂定为array
 *  public: string('0' || '1')  // 0不公开
 * }
 */

// 考虑是否要将currentRoom删除

const initUser = {
    id: -1,
    name: '',
    gender: '0',
    introduction: '',
    avator: 'img/user_avator/person_300.png'
};
const initRoomInof = {
    group_id: -1,
    group_name: '',
    group_intro: '',
    group_avator: 'img/group_avator/group.jpg',
    creator_id: 0,
    create_time: 0,
    admin: [{}],
    public: '0'
}
const initState = {
    islogin: false,
    user: initUser,
    currentItem: 'info',
    currentRoom: -1,
    currentRoomInfo: initRoomInof,
}
export default function reducer(preState = initState, action) {
    const { type, data } = action;
    switch (type) {
        case CHANGELOG: {
            return {...preState, islogin: data}
        }
            
        case CHANGEUSER: {
            let setData = data;
            if (data === undefined) {
                setData = initUser;
            }
            return {...preState, user: setData}
        }
            
        case CHANGECURRENTITEM: {
            return {...preState, currentItem: data}
        }
           
        case CHANGECURRENTROOM: {
            let setData = data;
            if (data === undefined) {
                setData = initState.currentRoom;
            }
            return {...preState, currentRoom: setData}
        }
            
        case CHANGECURRENTROOMINFO: {
            let setData = data;
            if (data === undefined) {
                setData = initRoomInof;
            }
            return {...preState, currentRoomInfo: setData}
        }
        default:
            return preState;
    }
}

